package cn.iocoder.yudao.module.erp.service.purpurchaseorderitem;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorderitem.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.purpurchaseorderitem.PurPurchaseOrderItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.purpurchaseorderitem.PurPurchaseOrderItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP 采购单项 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PurPurchaseOrderItemServiceImpl implements PurPurchaseOrderItemService {

    @Resource
    private PurPurchaseOrderItemMapper purPurchaseOrderItemMapper;

    @Override
    public Long createPurPurchaseOrderItem(PurPurchaseOrderItemSaveReqVO createReqVO) {
        // 插入
        PurPurchaseOrderItemDO purPurchaseOrderItem = BeanUtils.toBean(createReqVO, PurPurchaseOrderItemDO.class);
        purPurchaseOrderItemMapper.insert(purPurchaseOrderItem);
        // 返回
        return purPurchaseOrderItem.getId();
    }

    @Override
    public void updatePurPurchaseOrderItem(PurPurchaseOrderItemSaveReqVO updateReqVO) {
        // 校验存在
        validatePurPurchaseOrderItemExists(updateReqVO.getId());
        // 更新
        PurPurchaseOrderItemDO updateObj = BeanUtils.toBean(updateReqVO, PurPurchaseOrderItemDO.class);
        purPurchaseOrderItemMapper.updateById(updateObj);
    }

    @Override
    public void deletePurPurchaseOrderItem(Long id) {
        // 校验存在
        validatePurPurchaseOrderItemExists(id);
        // 删除
        purPurchaseOrderItemMapper.deleteById(id);
    }

    private void validatePurPurchaseOrderItemExists(Long id) {
        if (purPurchaseOrderItemMapper.selectById(id) == null) {
            throw exception(PUR_PURCHASE_ORDER_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public PurPurchaseOrderItemDO getPurPurchaseOrderItem(Long id) {
        return purPurchaseOrderItemMapper.selectById(id);
    }

    @Override
    public PageResult<PurPurchaseOrderItemDO> getPurPurchaseOrderItemPage(PurPurchaseOrderItemPageReqVO pageReqVO) {
        return purPurchaseOrderItemMapper.selectPage(pageReqVO);
    }

}